/**
 * @author salman.humdullah
 * @modifiedBy: Abid Hussain
 */ 
exports.GAGameLoader = Core.Class.singleton({
	classname: 'GAGameLoader',
	initialize : function() {
		this._url = null;
		this._isFailed = false;
		this._totalStores = 0;
		this._currentCounter = 0 ;
		this._responseObject = null;
		//this._userXp = userXp;
		//this._constructURL();
		GALocalizationHandler.instantiate();
		this._downloadedResourcesKey = GAConfigurations.getGameCode() + DOWNLOADED_RESOURCES;
		this._downloadedResources = {};
		var myself = this;
		this._storage = new Storage.KeyValue();
		this._storage.getItem(this._downloadedResourcesKey, 
			function(error, value, key) {
				if (!error) {
					myself._downloadedResources = JSON.parse(value);		
				}
			}
		);
	},
	
	addToDownloadedResources: function(key, resourceType) {
		if (this.isResourceAvailable(key, resourceType) == false) {
			this._downloadedResources[key+resourceType] = "1";
		}
	},
	
	persistDownloadedResources: function() {
		this._storage.setItem(this._downloadedResourcesKey, JSON.stringify(this._downloadedResources));
	},
	
	isResourceAvailable: function(key, resourceType) {
		var keyValue = this._downloadedResources[key+resourceType];
		if (typeof keyValue != 'undefined') {
			return true;
		}
		return false;	
	},
	
	setDelegate: function(delegate) {
		this._gameDelegate = delegate;
	},
	
	_constructURL : function(){
		var url = [];
		url.push(GAConfigurations.getURL());
		url.push("games/connect?unique_id=");
		//url.push(GAConfigurations.getUserId());
		url.push(GAConfigurations.getRecordID());
		//url.push("za33");
		//url.push("&XP=10"); //Zulu:::: change it to dynamic XP
		url.push("&XP="+this._userXp);
		
		url = encodeURI(url.join(""));
		this._url= url;
	},
	
	startConnect : function(userXp) {
		
		
		//zaki for local resources
		/*var connectResponse = kConnectResponse;
		
		var serverResponse = connectResponse._serverResponse;
		//console.log("zaki::::: serverresponse sending is "+JSON.stringify(serverResponse));
		
		
		var responseObject= new GAConnectResponse(serverResponse);
		responseObject.parseConnectResponse();
		//console.log("zaki::: response is"+responseObject);
		this.connectHandlerSuccess(responseObject);
		return;*/
		
		this._userXp = userXp;
		console.log("Zuluuu::: User XP "+userXp);
		this._constructURL();
		
		console.log("Zuluuu::: Connect URL is "+this._url);
		if( this._connectHandler == null){
			this._connectHandler = new GAConnectHandler(this._url, this);
		}
		this._connectHandler.fetchResponse();
	},
	getResponseObject : function() {
		return this._responseObject;
	},
////////////////****START****///////////////////
	connectHandlerSuccess : function(responseObject){
		console.log('Zuluuu:: ********* :: connectHandlerSuccess*********');
		//return;
		console.log('********* :: connectHandlerSuccess*********');
		this.showProgress(kGAGameLoadLevel.kGAGameLoadLevelConnectDone);
		this._responseObject = responseObject;
		console.log("Zuluuu::: server XP is "+this._responseObject._serverXp);
		//console.log("zaki:::: response is "+this._responseObject);
		//this.gameStateSuccess();
		//var temp = this._responseObject.getNeighborActions()
		//console.log("zaki:::::: CONNECT HANDLER SUCCESS FUNCTION"+this._responseObject.getNeoghborActions());
		GAGameStateHandler.startGameState(this,this._gameDelegate);
	},
	connectHandlerFailure : function(errorCode){
		console.log('Zuluuu::: ********* :: connectHandlerFailure*********');
		this._gameDelegate.onFailure(errorCode);
	},
/////////////////*****END******////////////////
	
	
//////////////*****Start******////////////////
	

	gameStateSuccess: function(){
		console.log('********* :: gameStateSuccess *********');
		this._storeloader = new GAStoreLoader(this._responseObject);
		this._storeloader.loadStores(this);
	},
	gameStateFailure: function(error){
		console.log('********* :: gameStateFailure *********');
		this._gameDelegate.onFailure(error);
	},
	
/////////////////*****END******////////////////	
	
//////////////*****Start******////////////////
	storeDownloaderSuccess : function (parseStores){
		console.log('************ :: storeDownloaderSuccess *********');
		this.showProgress(kGAGameLoadLevel.kGAGameLoadLevelStoreVersionsDone);
		var visibleIds = this._responseObject.getStoreVisibleIds();
		//console.log("zaki::::::: download Success"+visibleIds.length +"visible ids are "+visibleIds);
		if (visibleIds != null && visibleIds.length > 0) {
			//console.log("zaki:::: comes in here"+visibleIds);
			this._storeHandler = new GAStoreHandler(visibleIds, this.storeHandlerSuccess.bind(this), this.storeHandlerFailure.bind(this));
			this._storeHandler.startLoading();
		}
		else {
			this.storeHandlerSuccess();
		}
	},
	storeDownloaderFailure : function (errorMessage){
		console.log('********* :: storeDownloaderFailure*********');
		if(!this._isFailed){
			this._isFailed = true;
			this._gameDelegate.onFailure(errorMessage);
		}
	},
//////////////*****END******////////////////

//////////////*****START******////////////////
	storeHandlerSuccess : function (){
		this.showProgress(kGAGameLoadLevel.kGAGameLoadLevelStoreParsingDone);
		GAGameStateManager.setDelegate(this._gameDelegate);
		GAGameStateManager.startTimer(kGAGameState.kTimeOut);
		this._gameDelegate.onSuccess();
	},
	storeHandlerFailure: function (errorMessage, visibleId){
		console.log('********* storeParserFailure:: *********'+visibleId);
		var preferences = new Storage.KeyValue();
		preferences.setItem(visibleId, -1);
		this._gameDelegate.onFailure(errorMessage);
	},
//////////////*****END******////////////////
	showProgress : function(progressStatus) {
		this._gameDelegate.showProgress(progressStatus);
	},
	
	getStoreHandler: function() {
		return this._storeHandler;
	},

	destroy: function() {
		this._gameDelegate = null;
		this._storeloader.destroy();
		this._storeloader = null;
		this._connectHandler.destroy();
		this._connectHandler = null;
		this._storeHandler.destroy();
		this._storeHandler = null;
		this._url = null;
		this._isFailed = false;
		this._totalStores = 0;
		this._currentCounter = 0;
		this._responseObject = null;
		this._downloadedResourcesKey = null;
		this._downloadedResources = null;
		this._storage = null;
		GAGameStateHandler.destroy();
	}
});
